{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd \n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取datasheet信息\n",
    "datasheet_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st'\n",
    "os.chdir(datasheet_path)\n",
    "datasheet = pd.read_excel('DataSheet.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取online order1 & order2的被试名单\n",
    "online_order1_data = datasheet[datasheet['线上实验(Pavlovia)']==1]\n",
    "online_order2_data = datasheet[datasheet['线上实验(Pavlovia)']==2]\n",
    "online_order1_name = online_order1_data['被试姓名'].tolist()\n",
    "online_order2_name = online_order2_data['被试姓名'].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st\\NaoDao_DATA\\OnlineDATA(NaoDao)'\n",
    "\n",
    "ori_data_path = f'{base_path}/OriginalDATA'\n",
    "os.chdir(ori_data_path)\n",
    "online_ori_tasklists = os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['IAT', 'MentalRotation', 'MID', 'Multi-Tasking', 'Stroop']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "online_ori_tasklists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(online_ori_tasklists)):\n",
    "    online_task_path = f'{ori_data_path}/{online_ori_tasklists[i]}'\n",
    "    os.chdir(online_task_path)\n",
    "    # make two folders\n",
    "    os.mkdir('csv_file')\n",
    "    os.mkdir('log_file')\n",
    "    # move files\n",
    "    online_task_filelists = os.listdir()\n",
    "    # remove dir in filelists\n",
    "    online_task_filelists.remove('csv_file')\n",
    "    online_task_filelists.remove('log_file')\n",
    "    for m in range(0,len(online_task_filelists)):\n",
    "        if '.csv' in online_task_filelists[m]:\n",
    "            shutil.move(online_task_filelists[m], 'csv_file')\n",
    "        elif '.log' in online_task_filelists[m]:\n",
    "            shutil.move(online_task_filelists[m], 'log_file')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0,len(online_ori_tasklists)):\n",
    "    online_task_csv_path = f'{ori_data_path}/{online_ori_tasklists[i]}/csv_file'\n",
    "    os.chdir(online_task_csv_path)\n",
    "    original_csv_filelist = os.listdir()\n",
    "    for m in range(0,len(original_csv_filelist)):\n",
    "        if os.path.getsize(original_csv_filelist[m]) == 1:\n",
    "            os.remove(original_csv_filelist[m])\n",
    "    # get new filelist\n",
    "    new_csv_filelist = os.listdir()\n",
    "    # order1/2 folder path\n",
    "    order1_base_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st\\NaoDao_DATA\\OnlineDATA(NaoDao)\\OrderedDATA\\Order1'\n",
    "    order2_base_path = r'C:\\Users\\luke_\\Desktop\\[Projects]NaoDao_ClosedBeta_1st\\NaoDao_DATA\\OnlineDATA(NaoDao)\\OrderedDATA\\Order2'\n",
    "    order1_folder_path = f'{order1_base_path}/{online_ori_tasklists[i]}'\n",
    "    order2_folder_path = f'{order2_base_path}/{online_ori_tasklists[i]}'\n",
    "    for n in range(0, len(new_csv_filelist)):\n",
    "        each_csv = pd.read_csv(new_csv_filelist[n])\n",
    "        parti_name = each_csv['请填写您的姓名：'].iloc[0]\n",
    "        if parti_name in online_order1_name:\n",
    "            shutil.copy(new_csv_filelist[n],order1_folder_path)\n",
    "        elif parti_name in online_order2_name:\n",
    "            shutil.copy(new_csv_filelist[n],order2_folder_path)\n",
    "        else:\n",
    "            continue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rename file in ordered file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "order1_path = 'c:\\\\Users\\\\luke_\\\\Desktop\\\\[Projects]NaoDao_ClosedBeta_1st\\\\NaoDao_DATA\\\\OnlineDATA(NaoDao)\\\\OrderedDATA\\\\Order1'\n",
    "order2_path = 'c:\\\\Users\\\\luke_\\\\Desktop\\\\[Projects]NaoDao_ClosedBeta_1st\\\\NaoDao_DATA\\\\OnlineDATA(NaoDao)\\\\OrderedDATA\\\\Order2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# order1 rename file\n",
    "def new_rename(path_name,new_name):\n",
    "    try:\n",
    "        os.rename(path_name,new_name)\n",
    "    except Exception as e:\n",
    "        if e.args[0] == 17:\n",
    "            fname, fename = os.path.splitext(new_name)\n",
    "            new_rename(path_name,fname+'(1)'+fename)\n",
    "\n",
    "os.chdir(order1_path)\n",
    "task_lists = os.listdir()\n",
    "for i in range(0,len(task_lists)):\n",
    "    task_path = f'{order1_path}/{task_lists[i]}'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    name_lists = []\n",
    "    for file in filelists:\n",
    "        df = pd.read_csv(file)\n",
    "        name = df['请填写您的姓名：'].tolist()[0]\n",
    "        file_new_name = f'{name}_cleaned.csv'\n",
    "        new_rename(file,file_new_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# order2 rename file\n",
    "os.chdir(order2_path)\n",
    "task_lists = os.listdir()\n",
    "for i in range(0,len(task_lists)):\n",
    "    task_path = f'{order2_path}/{task_lists[i]}'\n",
    "    os.chdir(task_path)\n",
    "    filelists = os.listdir()\n",
    "    name_lists = []\n",
    "    for file in filelists:\n",
    "        df = pd.read_csv(file)\n",
    "        name = df['请填写您的姓名：'].tolist()[0]\n",
    "        file_new_name = f'{name}_cleaned.csv'\n",
    "        new_rename(file,file_new_name)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "07efdcd4b820c98a756949507a4d29d7862823915ec7477944641bea022f4f62"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
